<?php

class AlmacenController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
       /*dave*/
    }

    public function indexAction()
    {
     	# $this->_helper->layout->disableLayout ();
		//$this->_helper->viewRenderer->setNoRender ();
		 echo "indexActionlugaralmc";
	  
    }
	
	public function almacenAction(){
		echo $this->view->util()->registerScriptJSController($this->getRequest());
	}
	public function buscarAction(){
		$this->_helper->getHelper ( 'ajaxContext' )->initContext ();
		
		if ($this->getRequest ()->isXmlHttpRequest ()) {
			$this->_helper->viewRenderer->setNoRender ();
			$this->_helper->layout->disableLayout ();
			
			$upper = new Zend_Filter_StringToUpper();
			$upper->setEncoding('UTF-8');
			$tip = $this->_request->getPost('tip');
			$val = $upper->filter($this->_request->getPost('val'));
			$chk = $this->_request->getPost('chk');
			$cn = new Model_Data();
			$parameter[]=array("@pcod_almacen",($tip=='0' ? $val:''));
			$parameter[]=array("@pdesc_almacen",($tip=='1' ? $val:''));
			$parameter[]=array("@pest_almacen",$chk);
			$result_bus = $cn ->ejec_store_procedura_sql("MAlmacen_list", $parameter);

			if($result_bus==null || count($result_bus)==0){
				echo "Sin Resultados";
			}else{
				$tb="";
				foreach ($result_bus as $value) {
					$tb .= '<tr id="'.$value[0].'">';
					$tb .='<td>'.$value[0].'</td>';
					$tb .='<td>'.$value[1].'</td>';
					$tb .='<td>'.$value[2].'</td>';
					$tb .='<td>'.$value[3].'</td>';
					$tb .= '</tr>';
				}
				//echo '<table border="1"><tr><td>';
				echo '<table id="tbl_result" class="ui-widget ui-widget-content tablesorter">'.
						'<thead>'.
							'<tr class="ui-widget-header">'.
								'<tr class="ui-widget-header">'.
									'<th width="100px">Codigo</th>'.
									'<th width="150px">Almacen</th>'.
									'<th width="100px">Direccion</th>'.
									'<th width="130px">Observacion</th>'.
								'</tr>'.
							'</tr>'.
						'</thead>'.
						'<tbody>'.
							$tb.
						'</tbody>'.
					'</table>';

				//echo "</td></tr><tr><td>";
				echo $this->view->TableSorter()->ScriptDivPage();
				//echo $this->view->util()->registerScriptJSController($this->getRequest());
				//echo "</td></tr></table>";
				//$(\"#tbl_result\").tablesorter({sortList:[[0,0]], widgets: ['zebra'],headers: { 5:{sorter: false}}}).tablesorterPager({container: $(\"#pager\")});
				echo "<script type=\"text/javascript\">
						$(function() {		
							$(\"#tbl_result\").tablesorter({sortList:[[0,0]]})
							$(\"#tbl_result\").tablesorterPager({
								container: $(\"#pager\"),
								afterPagingCallBack: function(){
									AbrirMenuMateriaPrima();
								}
								});
							
						});	
					</script>";
				
				
			}
			
		}
	}
	
	public function mantenimientoAction(){
		
     	echo $this->view->util()->registerScriptJSController($this->getRequest());
		$this->_helper->layout->disableLayout ();
		#$this->_helper->viewRenderer->setNoRender ();
		$type = $this->_request->getParam('type', '');
		$cod = $this->_request->getParam('codItem', '');
		$cn = new Model_Data();
		$pintar = New Libreria_Pintar();
		$parameter[]=array("@pcod_almacen",$cod);
		$parameter[]=array("@pdesc_almacen",'');
		$parameter[]=array("@pest_almacen",'');
	
		
		if ($type=='N'){
			
		}elseif($type=='M' || $type=='E'){
			$result_bus = $cn ->ejec_store_procedura_sql("MAlmacen_list", $parameter);
			$val[] = array('#hdcodigo',$result_bus[0][0],'val');
			$val[] = array('#txtds_almacen',$result_bus[0][1],'val');
			$val[] = array('#txtds_direc',$result_bus[0][2],'val');
			$val[] = array('#txtds_obs',$result_bus[0][3],'val');
			
			
			$parameterdt[]=array("@pcod_sector",'');
			$parameterdt[]=array("@pdesc_sector",'');
			$parameterdt[]=array("@pest_sector",'1');
			$parameterdt[]=array("@pcod_almacen",$cod);
			
			$result_det = $cn ->ejec_store_procedura_sql("MSector_list", $parameterdt);
			$detallemp='';
			if (count($result_det)==0){
				
			}else{
			for ($i=0;$i<count($result_det);$i++){
				$detallemp .=
				'<tr >'.
					'<td>'.
						'<input type="text"  name="det['.$i.'][txt_descrip]" value="'.$result_det[$i][3].'"  class="ui-widget-content ui-corner-all">'.
					'</td>'.
					'<td>'.
						'<input type="text"  name="det['.$i.'][txt_obs]"  value="'.$result_det[$i][4].'" class="ui-widget-content ui-corner-all">'.							
					'</td> '.
					'<td style="display:none" ><input  name="det['.$i.'][tblcodigo]" id="tblcodigo" type="text" value="'.$result_det[$i][0].'"></td>'.
				'</tr>';
			}
				$val[] = array('#tbl_detalleAL tbody',$detallemp,'html');
				
			}
			$pintar->PintarValor($val);
		}/*elseif($type=='E'){
			$result_bus = $cn ->ejec_store_procedura_sql("MLugarAlmacenamiento_list", $parameter);
			$val[] = array('#hdcodigo',$result_bus[0][0],'val');
			$val[] = array('#txtds_apepat',$result_bus[0][5],'val');
			$val[] = array('#txtnum_apemat',$result_bus[0][6],'val');
			$val[] = array('#txtds_nom',$result_bus[0][7],'val');
			$val[] = array('#cbocod_tipodoc',$pintar->ContenidoCombo($cboCB,$result_bus[0][2]),'html');
			$val[] = array('#txtnum_doc',$result_bus[0][4],'val');
			$val[] = array('#txtds_direc',$result_bus[0][8],'val');
			$val[] = array('#txtnu_telf',$result_bus[0][9],'val');
			
		}*/
		
		
		echo '<script type="text/javascript">'.
						'$("#tbl_detalleAL").grider({'.
						'countRow : true,'.
						'countRowAdd : true'.
						"});</script>";
	}

	public function executemanteAction(){
		$this->_helper->getHelper ( 'ajaxContext' )->initContext ();
		
		if ($this->getRequest ()->isXmlHttpRequest ()) {
			$this->_helper->viewRenderer->setNoRender ();
			$this->_helper->layout->disableLayout ();
			
			$upper = new Zend_Filter_StringToUpper();
			$upper->setEncoding('UTF-8');
			$tip = $this->_request->getPost('type');
			$hdcodigo = $upper->filter($this->_request->getPost('hdcodigo'));
			$txtds_almacen = $upper->filter($this->_request->getPost('txtds_almacen'));
			$txtds_direc = $upper->filter($this->_request->getPost('txtds_direc'));
			$txtds_obs = $upper->filter($this->_request->getPost('txtds_obs'));
			$st_estado = $upper->filter($this->_request->getPost('st_stado'));
			$arrtable = $this->_request->getPost('det');
			
			$cn = new Model_Data();
			
			if($tip=='M' ||$tip=='N' ){
				$parameter[]=array("@ptype",$tip);
				$parameter[]=array("@pcod_almacen",$hdcodigo);
				$parameter[]=array("@pdesc_almacen",$txtds_almacen);
				$parameter[]=array("@pdesc_direccion",$txtds_direc);
				$parameter[]=array("@pobs_almacen" ,$txtds_obs);
				$parameter[]=array("@pest_almacen" ,$st_estado);
				$parameter[]=array("@puser_reg",$this->view->util()->getUser());
				$parameter[]=array("@phost_reg",$this->view->util()->getHost());
				$xml='';
				$i=1;
				/*  @pcod_almacen  char(5),      
					@pdesc_almacen  varchar(150),      
					@pdesc_direccion varchar(250),      
					@pobs_almacen varchar(250),      
					@pest_almacen  char(1),     
					@pdetalle*/
				foreach ($arrtable as $value) {
					$xml.='';
					$xml .='<r ';
					$xml .='xcodigo="'.$value["tblcodigo"].'" ';
					$xml .='xcorrela="'.($i).'" ';
					$xml .='xcod_almacen="'.$hdcodigo.'" ';
					$xml .='xdesc_sector="'.$value["txt_descrip"].'" ';
					$xml .='xdesc_obs="'.$value["txt_obs"].'" ';
					$xml .='>';
					$xml .='</r>';
					$i++;
				}
				
				$xml="<d>".$xml."</d>";
				//echo "<textarea>".$xml."</textarea>";
				$parameter[]=array("@pdetalle",$xml);
				$result = $cn ->ejec_store_procedura_sql("MAlmacen_InsertUpdate", $parameter);
				 	
			}elseif($tip=='D'){
	
				$parameter[]=array("@pcod_almacen",$hdcodigo);
				$parameter[]=array("@puser_reg",$this->view->util()->getUser());
				$parameter[]=array("@phost_reg",$this->view->util()->getHost());
				$result = $cn ->ejec_store_procedura_sql("MAlmacen_Delete", $parameter);
			}
			if($result[0][0]=='1') echo "Transaccion Exitosa";
			if($result[0][0]=='2') 
			 echo  $this->view->util()->getTextError($result[0]);
		}
		
	}

	public function selectalmacenAction(){
		
		$this->_helper->layout->disableLayout ();
		echo $this->view->util()->registerScriptJSController($this->getRequest());
		$CBparameter[]=array("@pcod_almacen","");
		$CBparameter[]=array("@pdesc_almacen",'');
		$CBparameter[]=array("@pest_almacen",'1');
		$cn = new Model_Data();
		$pintar = New Libreria_Pintar();
		$CBresult_bus = $cn ->ejec_store_procedura_sql("MAlmacen_list", $CBparameter);
		for ($x=0;$x<count($CBresult_bus);$x++)
    		$cboCB[] = array($CBresult_bus[$x][0],$CBresult_bus[$x][1]);
		
		$val[] = array('#cbocod_almacen',$pintar->ContenidoCombo($cboCB,'null'),'html');
		$pintar->PintarValor($val);
	} 

}

